from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdksts.request.v20150401 import AssumeRoleRequest
import json


def getStsToken():
    
    REGINID = 'cn-beijing'
    ENDPOINT = 'sts.cn-beijing.aliyuncs.com'
    region_provider.add_endpoint('Sts', REGINID, ENDPOINT)
    
    clt = client.AcsClient('LTAIcWKeZ0kAaMNF', 'Kax2Hx1Yf5cqi5nwmg4q5GSNEphTX2', REGINID)
    
    req = AssumeRoleRequest.AssumeRoleRequest()
    
    req.set_RoleArn('acs:ram::1160593523356401:role/aliyunosstokengeneratorrole')
    
    req.set_RoleSessionName('external-username')
    #req.set_DurationSeconds(Integer) # past time
    #req.set_Policy('')
    
    resp = clt.do_action_with_exception(req)
    #print resp
    print type(resp)
    resp = json.loads(resp)
    print type(resp)
    token = dict(status='200', AccessKeyId=resp['Credentials']['AccessKeyId'], AccessKeySecret=resp['Credentials']['AccessKeySecret'], SecurityToken=resp['Credentials']['SecurityToken'], Expiration=resp['Credentials']['Expiration'])
    
    tokenString = json.dumps(token)
    #print tokenString
    
    return tokenString
    

